timestamp <- Sys.time()
library(caret)
library(plyr)
library(recipes)
library(dplyr)

model <- "cubist"



#########################################################################

library(caret)
library(plyr)
library(recipes)
library(dplyr)
set.seed(1)
training <- SLC14_1(30)
testing <- SLC14_1(100)
trainX <- training[, -ncol(training)]
trainY <- training$y

rec_reg <- recipe(y ~ ., data = training) %>%
  step_center(all_predictors()) %>%
  step_scale(all_predictors()) 
testX <- trainX[, -ncol(training)]
testY <- trainX$y 

seeds <- vector(mode = "list", length = nrow(training) + 1)
seeds <- lapply(seeds, function(x) 1:20)

rctrl1 <- trainControl(method = "cv", number = 3, returnResamp = "all", seeds = seeds)
rctrl2 <- trainControl(method = "LOOCV", seeds = seeds)
rctrl3 <- trainControl(method = "none", seeds = seeds)
rctrlR <- trainControl(method = "cv", number = 3, returnResamp = "all", search = "random")

set.seed(849)
test_reg_cv_model <- train(trainX, trainY, method = "cubist", 
                           trControl = rctrl1,
                           control = Cubist::cubistControl(seed = 1))
test_reg_pred <- predict(test_reg_cv_model, testX)


set.seed(849)
test_reg_cv_form <- train(y ~ ., data = training, 
                          method = "cubist", 
                          trControl = rctrl1,
                          control = Cubist::cubistControl(seed = 1))
test_reg_pred_form <- predict(test_reg_cv_form, testX)


set.seed(849)
test_reg_loo_model <- train(trainX, trainY, method = "cubist", 
                            trControl = rctrl1,
                            control = Cubist::cubistControl(seed = 1))

set.seed(849)
test_reg_rand <- train(trainX, trainY, 
                       method = "cubist", 
                       trControl = rctrlR,
                       tuneLength = 4)

set.seed(849)
test_reg_none_model <- train(trainX, trainY, 
                             method = "cubist", 
                             trControl = rctrl3,
                             tuneGrid = data.frame(committees = 5, neighbors = 3),
                             preProc = c("center", "scale"),
                             control = Cubist::cubistControl(seed = 1))
test_reg_none_pred <- predict(test_reg_none_model, testX)

set.seed(849)
test_reg_rec <- train(x = rec_reg,
                      data = training,
                      method = "cubist", 
                      trControl = rctrl1,
                      control = Cubist::cubistControl(seed = 1))

test_reg_pred_rec <- predict(test_reg_rec, testing[, -ncol(testing)])

#########################################################################

test_reg_imp <- varImp(test_reg_cv_model)

#########################################################################

sInfo <- sessionInfo()
timestamp_end <- Sys.time()

tests <- grep("test_", ls(), fixed = TRUE, value = TRUE)

save(list = c(tests, "sInfo", "timestamp", "timestamp_end"),
     file = file.path(getwd(), paste(model, ".RData", sep = "")))

if(!interactive())
   q("no")


